#!/usr/bin/env python




import time
from optparse import OptionParser
from multiprocessing import Process

from dexen.system.common import service
from dexen.system.server import archive_server
from dexen.system.server import service as srv_svc
from dexen.system.registry import service as reg_svc

def start():
    parser = OptionParser("dexen-server [OPTIONS]...")
    parser.add_option("-n", "--name", dest="reg_name", 
                      default="unassigned", metavar="SERVER_NAME",
                      help="gives logical name to the server.")
    parser.add_option("-v", "--verbose", 
                      action="store_true", dest="verbose", default=False,
                      help="Print all the system messages")
    (options, args) = parser.parse_args()
    if len(args) > 0:
        parser.error("Positional argument is not expected.")
    is_quiet = not options.verbose
    
    reg = Process(target=reg_svc.start, name="DEXEN_REG", 
                  args=(options.reg_name, is_quiet))
    reg.start()
    
    arc_server = Process(target=archive_server.start, 
                         name="DEXEN_ARCHIVE_SERVER")
    arc_server.start()
    print "Dexen server is starting on %s."%(service.get_my_ip())
    srv_svc.start(options.reg_name, is_quiet)

def main():
    start()

if __name__ == "__main__":
    main()